#!/bin/bash -
# Copyright 2012 E.Fagnoni from the original work Colin of Johnson  . All rights reserved.
# see https://github.com/colinbjohnson/aws-missing-tools
# 
# Redistribution and use in source and binary forms, with or without modification, are
# permitted provided that the following conditions are met:
# 
#    1. Redistributions of source code must retain the above copyright notice, this list of
#       conditions and the following disclaimer.
# 
#    2. Redistributions in binary form must reproduce the above copyright notice, this list
#       of conditions and the following disclaimer in the documentation and/or other materials
#       provided with the distribution.
# 
# THIS SOFTWARE IS PROVIDED  ``AS IS'' AND ANY EXPRESS OR IMPLIED
# WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
# FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL Benjie Gillam OR
# CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
# SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
# ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
# NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
# ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
# 
# The views and conclusions contained in the software and documentation are those of the
# authors and should not be interpreted as representing official policies, either expressed
# or implied, of Authors.

if [ -f /etc/aws/env.conf ]; then
	. /etc/aws/env.conf
else
	echo "invalid config file"
	exit 1;
fi
if [ ! -f $AWS_CREDENTIAL_FILE ]; then
	echo "Missing or unreadable AWS credential file"
	exit 1;
fi

if [ -z "$AWS_CLOUDWATCH_HOME" ]; then echo "Config error"; exit 1; fi

#set  defaults if not defined in config file
if [ -z "$AWS_SERVER" ]; then AWS_SERVER="169.254.169.254"; fi
if [ -z "$AWS_NAMESPACE" ]; then AWS_NAMESPACE="eCloud"; fi
if [ -z "$AWS_DIMENSIONS" ]; then AWS_DIMENSIONS="eLinux"; fi

#get instance id - used for putting metric
INSTANCE_ID=`curl -s http://$AWS_SERVER/latest/meta-data/instance-id`
INSTANCE_AZ=`curl -s curl http://$AWS_SERVER/latest/meta-data/placement/availability-zone/`
INSTANCE_REGION=${INSTANCE_AZ%?}

#could be done using "free" or "vmstat" - use of less and grep is believed to provide widest compatibility - CJ 2011-11-24
memfree=`cat /proc/meminfo | grep -i MemFree | grep -o [0-9]*`
swaptotal=`cat /proc/meminfo | grep -i SwapTotal | grep -o [0-9]*`
swapfree=`cat /proc/meminfo | grep -i SwapFree | grep -o [0-9]*`
swapused=$(($swaptotal-$swapfree))

export AWS_CLOUDWATCH_HOME AWS_CREDENTIAL_FILE JAVA_HOME
#mon-put-data to put metrics
$AWS_CLOUDWATCH_HOME/bin/mon-put-data --region $INSTANCE_REGION --metric-name MemoryFree --namespace $AWS_NAMESPACE --value $memfree --unit Kilobytes --dimensions "$AWS_DIMENSIONS=$INSTANCE_ID"
$AWS_CLOUDWATCH_HOME/bin/mon-put-data --region $INSTANCE_REGION --metric-name SwapUsed --namespace $AWS_NAMESPACE --value $swapused --unit Kilobytes --dimensions "$AWS_DIMENSIONS=$INSTANCE_ID"

#to run in cron every 5 minutes - note that you must first provide credentials for mon-put-data
#echo "*/5 * * * * root /usr/local/bin/cw-write-memory-metrics.sh" > /etc/cron.d/cw-write-memory-metrics